#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

bool carPooling(vector<vector<int>>& trips, int capacity) {
    sort(trips.begin(), trips.end(), [](const vector<int>& a, const vector<int>& b) {
        if (a[1] != b[1])
            return a[1] < b[1];
        else
            return a[2] < b[2];
        });

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    int cur = 0;
    for (auto it : trips)
    {
        while (!pq.empty())
        {
            int t = pq.top().first;
            if (t <= it[1])
            {
                cur -= pq.top().second;
                pq.pop();
            }
            else
                break;
        }

        cur += it[0];
        if (cur > capacity)
            return false;
        pq.push({ it[2],it[0] });
    }
    return true;
}

int main()
{
    vector<vector<int>> vv = { {9,3,4},{9,1,7},{4,2,4},{7,4,5} };
    carPooling(vv, 23);
	return 0;
}